package com.singzi.leetcode.N300;

import java.util.*;

public class T0239 {
    public static void main(String[] args) {
        int []nums = {1,3,45,56,6,7,87};
        Solution0239 solution0239 = new Solution0239();
        solution0239.maxSlidingWindow(nums, 6);
    }
}
class Solution0239 {
    public int[] maxSlidingWindow(int[] nums, int k) {
        TreeMap<Integer, Integer> treeMap = new TreeMap<>();
        int n = nums.length - k + 1;
        int []ans = new int[n];
        int index = 0;

        for(int i = 0;i < k;i++){
            treeMap.put(nums[i], treeMap.getOrDefault(nums[i], 0)+1);
        }
        while (index < n){
            ans[index] = treeMap.lastKey();
            treeMap.put(nums[index], treeMap.get(nums[index])-1);
            if(treeMap.get(nums[index]) == 0) treeMap.remove(nums[index]);
            treeMap.put(nums[index+k], treeMap.getOrDefault(nums[index+k], 0)+1);
            index++;
        }
        return ans;

    }
}